
package controllers;

import controllers.filters.FileReaderFilter;
import controllers.filters.PersistenceFilter;
import controllers.filters.SenderFilter;
import controllers.filters.SyntacticValidatorFilter;
import controllers.pipes.Persistence2SenderPipe;
import controllers.pipes.Reader2ValidatorPipe;
import controllers.pipes.Validator2PersistencePipe;
import entities.Archive;
import javafx.concurrent.Task;
import view.LoadFilePane;

/**
 *
 * @author john
 */
public class CheckSyntacticValidationProgressTask extends Task{
    
    private final String filePath;
    private FileReaderFilter theFileReaderThread;
    private SyntacticValidatorFilter theValidatorThread;
    
    public CheckSyntacticValidationProgressTask(String filePath) {
        this.filePath=filePath;
        theFileReaderThread = new FileReaderFilter(); // create a new thread
        theValidatorThread = new SyntacticValidatorFilter(); // create a new thread
    }

    public String getFilePath() {
        return filePath;
    }

    public FileReaderFilter getTheFileReaderThread() {
        return theFileReaderThread;
    }

    public SyntacticValidatorFilter getTheValidatorThread() {
        return theValidatorThread;
    }
    
    @Override
    protected Object call() throws Exception {//method
        try {
//          //comment from here
          Persistence2SenderPipe instance3 = Persistence2SenderPipe.getInstance();//initialize the queue to avoid thread-driven duplicity of the singleton
          Reader2ValidatorPipe instance = Reader2ValidatorPipe.getInstance();//initialize the queue to avoid thread-driven duplicity of the singleton
          Validator2PersistencePipe instance2 = Validator2PersistencePipe.getInstance();//initialize the queue to avoid thread-driven duplicity of the singleton
          SenderFilter theSenderThread = new SenderFilter(); // create a new thread
          PersistenceFilter thePersistenceThread = new PersistenceFilter(); // create a new thread
          theFileReaderThread.setFilePath(filePath);
          long LinesOfFile = LoadFilePane.getNumLines();
          System.out.println(LinesOfFile);
          theSenderThread.setLines(LinesOfFile);
          thePersistenceThread.setLines(LinesOfFile);
          SyntacticValidator validator = new SyntacticValidator();
          //TODO: Juan. Aquie comienza Pipes & Filter
          Archive archive = validator.getArchive(filePath, 1001, 1);
          thePersistenceThread.setArchive(archive);
          theValidatorThread.setLines(LinesOfFile);
          theFileReaderThread.start();
          new Thread(theValidatorThread).start();
//          theValidatorThread.start();
          thePersistenceThread.start();
          theSenderThread.start();
          //<editor-fold defaultstate="collapsed" desc="for deleting">
          //to here in order to disable pipes and filters
          
          //
          //
          //
          //          SyntacticValidator validator = new SyntacticValidator();
          //          FileInputStream fstream = new FileInputStream(filePath);
          //          // Get the object of DataInputStream
          //          DataInputStream in = new DataInputStream(fstream);
          //          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          //          String strLine;
          //          //Read File Line By Line
          ////                    long startTime = System.nanoTime();
          //
          //          //Se carga el archivo con la información de la ruta, del
          //          //establecimiento y de la marca. Estos dos últimos se deben
          //          //settear en el siguiente método. Eje: 1001, 1
          //          Archive archive = validator.getArchive(filePath, 1001, 1);
          //          int count = 0;
          //          int correct = 0;
          //          int wrong = 0;
          //          while ((strLine = br.readLine()) != null) {
          //            count++;
          //            if (validator.validate(strLine, archive, count)) {
          //              correct++;
          //              updateMessage("Línea " + count + " correcta.");
          //            } else {
          //              wrong++;
          //              updateMessage("¡La línea " + count + " es incorrecta! No tiene el formato adecuado.");
          //            }
          //            if (count == totalLinesNumber) {
          //              totalSendLinesNumber -= wrong;
          //            }
          //            updateProgress(count, totalLinesNumber);
          //            waitSomeTime();
          //            if (isCancelled()) {
          //              break;
          //            }
          //          }
          ////                    System.out.println("Good ones: " + correct);
          ////                    System.out.println("Spoilt ones: " + wrong);
          ////                    System.out.println("Total:  " + (wrong + correct));
          ////                    long estimatedTime = System.nanoTime() - startTime;
          ////                    System.out.println("Time:  " + ((float) estimatedTime / (float) 1000000000) + " segundos");
          //          //Close the input stream
          //          in.close();
          //        } catch (IOException | InterruptedException e) {
          //          System.err.println("Error: " + e.getMessage());
          //</editor-fold>
        } catch (Exception crap) {
          updateMessage("Shit!");
        }
        return true;
      }
    
}